import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useMaloginStore = defineStore('malogin', () => {
  // 状态管理
  const isLoggedIn = ref(false)
  const username = ref('')
  const token = ref('')

  // 计算属性
  const userInfo = computed(() => ({
    isLoggedIn: isLoggedIn.value,
    username: username.value,
    token: token.value
  }))

  // 登录操作
  function login(user: string, password: string) {
    if (user === 'ma' && password === '123456') {
      isLoggedIn.value = true
      username.value = user
      token.value = 'mock-token'
      return true
    } else {
      return false
    }
  }

  // 注销操作
  function logout() {
    isLoggedIn.value = false
    username.value = ''
    token.value = ''
  }

  return { isLoggedIn, username, token, userInfo, login, logout }
})